package com.kui.trydoto.springmail;

import org.junit.Test;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class ExampleTest {

    private String toMail = SelfProperties.toMail;
    private String ccMail = SelfProperties.ccMail;

    /**
     * 基本 MailSender 和 SimpleMailMessage 用法
     */
    @Test
    public void test_mailSender() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 发件人
        simpleMailMessage.setFrom(SelfProperties.mailUserName);
        // 收件人
        simpleMailMessage.setTo(toMail);
        // 抄送
        simpleMailMessage.setCc(ccMail);
        // 邮件主题
        simpleMailMessage.setSubject("这是一封测试邮件002");
        // 邮件正文
        simpleMailMessage.setText("今天是2024年6月22日，我现在的位置是：海淀北部图书馆，现在时间是：北京时间10点37分，今天天气晴！星期六");
        simpleMailMessage.setSentDate(new Date());

        MailSender mailSender = EmailUtil.getMailSender();
        mailSender.send(simpleMailMessage);
    }


    /**
     * 使用 JavaMailSender 和 MimeMessagePreparator
     */
    @Test
    public void test_MimeMessagePreparator() {
        MimeMessagePreparator mimeMessagePreparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setFrom(SelfProperties.mailUserName);
                mimeMessage.setRecipients(Message.RecipientType.TO, toMail);
                mimeMessage.setRecipient(Message.RecipientType.CC, new InternetAddress(ccMail));
                mimeMessage.setSubject("这是一封测试邮件，通过 MimeMessagePreparator 进行发送！");
                mimeMessage.setText("今天是2024年6月22日，我现在的位置是：海淀北部图书馆，现在时间是：北京时间10点37分，今天天气晴！星期六", "UTF-8");
            }
        };
        JavaMailSenderImpl mailSenderImpl = EmailUtil.getQQJavaMailSenderImpl();
        mailSenderImpl.send(mimeMessagePreparator);
    }


    /**
     * 使用 JavaMail MimeMessageHelper
     */
    @Test
    public void test_MimeMessageHelper() {
        JavaMailSenderImpl mailSenderImpl = EmailUtil.getQQJavaMailSenderImpl();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSenderImpl.createMimeMessage(), false);
            messageHelper.setFrom(SelfProperties.mailUserName);
            messageHelper.setTo(toMail);
            messageHelper.setCc(new InternetAddress(ccMail));
            messageHelper.setSubject("这是一封测试邮件，通过 MimeMessageHelper 进行构建！");
            messageHelper.setText("今天是2024年6月22日，我现在的位置是：海淀北部图书馆，现在时间是：北京时间10点37分，今天天气晴！星期六");

            mailSenderImpl.send(messageHelper.getMimeMessage());
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送附件
     */
    @Test
    public void test_addAttachment() {
        JavaMailSenderImpl mailSenderImpl = EmailUtil.getQQJavaMailSenderImpl();
        try {
            // use the true flag to indicate you need a multipart message
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSenderImpl.createMimeMessage(), true);
            messageHelper.setFrom(SelfProperties.mailUserName);
            messageHelper.setTo(toMail);
            messageHelper.setCc(new InternetAddress(ccMail));
            messageHelper.setSubject("这是一封测试邮件，尝试添加附件！");
            messageHelper.setText("今天是2024年6月22日，我现在的位置是：海淀北部图书馆，现在时间是：北京时间10点37分，今天天气晴！星期六");

            InputStream pic1 = getClass().getClassLoader().getResourceAsStream("images/dancedog.png");
            InputStream pic2 = getClass().getClassLoader().getResourceAsStream("images/dancecat.png");

            messageHelper.addAttachment("跳舞的小狗.png", new ByteArrayDataSource(pic1, "application/octet-stream"));
            messageHelper.addAttachment("跳舞的小猫.png", new ByteArrayDataSource(pic2, "application/octet-stream"));

            mailSenderImpl.send(messageHelper.getMimeMessage());
        } catch (MessagingException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送内联资源
     */
    @Test
    public void test_inlineResource() {
        JavaMailSenderImpl mailSenderImpl = EmailUtil.getQQJavaMailSenderImpl();
        try {
            // use the true flag to indicate you need a multipart message
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSenderImpl.createMimeMessage(), true);
            messageHelper.setFrom(SelfProperties.mailUserName);
            messageHelper.setTo(toMail);
            messageHelper.setCc(new InternetAddress(ccMail));
            messageHelper.setSubject("这是一封测试邮件，尝试添加内联资源！");

            messageHelper.setText("<html><body>" +
                    "<h2>一直会跳舞的猫<h2/><img src='cid:r001'/>" +
                    "<h2>一直会跳舞的狗<h2/><img src='cid:r002'/>" +
                    "</body></html>", true);

            InputStream pic1 = getClass().getClassLoader().getResourceAsStream("images/dancedog.png");
            InputStream pic2 = getClass().getClassLoader().getResourceAsStream("images/dancecat.png");

            messageHelper.addInline("r001", new ByteArrayDataSource(pic1, "application/octet-stream"));
            messageHelper.addInline("r002", new ByteArrayDataSource(pic2, "application/octet-stream"));

            mailSenderImpl.send(messageHelper.getMimeMessage());
        } catch (MessagingException | IOException e) {
            throw new RuntimeException(e);
        }
    }

}
